package com.yl.chainStore.control.pay

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.entity.pay.PayOrderDiscountRule
import com.yl.chainStore.payrule.common.RuleField
import com.yl.chainStore.payrule.order.OrderRuleTypeEnum
import com.yl.chainStore.pojo.vo.pay.OrderDiscountRuleSelectVo
import com.yl.chainStore.pojo.vo.pay.PayOrderDiscountRuleSelectVo
import com.yl.chainStore.services.IWebCompanyDeptService
import com.yl.chainStore.services.pay.IPayOrderDiscountRuleService
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.annotations.PermsCode
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("pay/order_discount")
open class PayOrderDiscountRuleController(
	private val iWebCompanyDeptService: IWebCompanyDeptService,
	private val iPayOrderDiscountRuleService: IPayOrderDiscountRuleService
) {

	/**
	 * 获取分页
	 */
	@PostMapping("getOrderDiscountRulePage")
	@PermsCode("pay:order:discount:setting:list")
	@Transactional
	open fun getOrderDiscountRulePage(@RequestBody vo: PayOrderDiscountRuleSelectVo): ResponseBody {
		vo.subCompanyId = getContext().subCompanyId
		val page = iWebCompanyDeptService.getOrderPayDiscountRulePage(Page(vo.current, vo.size), vo)
		return returnSuccess(page)
	}

	/**
	 * 添加规则
	 */
	@PostMapping("addOrderDiscountRule")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("pay:order:discount:setting:add")
	open fun addOrderDiscountRule(@RequestBody rule: PayOrderDiscountRule):ResponseBody{
		val addItemPayDiscountRule = iWebCompanyDeptService.addOrderPayDiscountRule(rule)
		if(addItemPayDiscountRule.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return addItemPayDiscountRule
	}

	/**
	 * 修改
	 */
	@PostMapping("updateOrderDiscountRule")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("pay:order:discount:setting:update")
	open fun updateOrderDiscountRule(@RequestBody rule: PayOrderDiscountRule):ResponseBody{
		val updateInfo = iPayOrderDiscountRuleService.updateInfo(rule)
		if(updateInfo.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		if(rule.other != null){
			val updateRule = iPayOrderDiscountRuleService.updateRule(rule.id, rule.other!!)
			if(updateRule.bodyIsError()){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			}
		}
		val resetTime = iPayOrderDiscountRuleService.resetTime(rule.id, rule.startTime, rule.endTime)
		if(resetTime.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return returnSuccess()
	}


	/**
	 * 开启
	 */
	@PostMapping("openOrderDiscountRule/{id}")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("pay:order:discount:setting:updateStatus")
	open fun openOrderDiscountRule(@PathVariable id :Long):ResponseBody{
		val openRule = iPayOrderDiscountRuleService.openRule(id)
		if(openRule.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return openRule
	}

	/**
	 * 关闭
	 */
	@PostMapping("closeOrderDiscountRule/{id}")
	@PermsCode("pay:order:discount:setting:updateStatus")
	@Transactional(rollbackFor = [Exception::class])
	open fun closeOrderDiscountRule(@PathVariable id :Long):ResponseBody{
		val closeRule = iPayOrderDiscountRuleService.closeRule(id)
		if(closeRule.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return closeRule
	}

	@PostMapping("delOrderDiscountRule/{id}")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("pay:order:discount:setting:del")
	open fun delOrderDiscountRule(@PathVariable id :Long):ResponseBody{
		val del = iPayOrderDiscountRuleService.del(id)
		if(del.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return del
	}

	/**
	 * 获取订单优惠规则种类
	 */
	@GetMapping("getOrderTypeSelect")
	@Transactional
	open fun getCouponsTypeSelect(): ResponseBody {
		val list = mutableListOf<Map<String, Any>>()
		for (item in OrderRuleTypeEnum.values()) {
			if (item == OrderRuleTypeEnum.DEFAULT) {
				continue
			}
			val map = mutableMapOf<String, Any>()
			map["id"] = item.typeName
			map["name"] = item.typeName
			list.add(map)
		}
		return returnSuccess(list)
	}

	/**
	 * 获取订单优惠配置
	 */
	@PostMapping("getOrderTypeOption")
	@Transactional
	open fun getItemTypeOption(@RequestBody vo: OrderDiscountRuleSelectVo): ResponseBody {
		val list = mutableListOf<Map<String, Any>>()
		val enum = vo.ruleType
		val fs = enum.clazz.declaredFields
		try {
			for (f in fs) {
				val showField: RuleField = f.getAnnotation(RuleField::class.java) ?: continue
				val map = mutableMapOf<String, Any>()
				map["name"] = f.name
				map["viewName"] = showField.viewName
				map["dataSource"] = showField.dataSource
				map["must"] = showField.must
				map["type"] = showField.type
				map["value"] = showField.value
				list.add(map)
			}
		} catch (e: java.lang.Exception) {
			e.printStackTrace()
		}
		return returnSuccess(list)
	}
}
